﻿@model IEnumerable<MasResourceSubGroup>

@{
    int resGrpId = ViewBag.ResGrpId;
    var resGrp = new KPIS.GERP.WEB.GIMS.MasResourceService.MasResourceServiceClient().SelectResourceGroup(new MasResourceGroup{ResGrpId = resGrpId}, null).FirstOrDefault();
    var resType = new KPIS.GERP.WEB.GIMS.MasResourceService.MasResourceServiceClient().SelectResourceType(new MasResourceType{ResTypeId = resGrp.ResTypeId.Value}, null).FirstOrDefault();
    ViewBag.CustomMenu = new Dictionary<string, string>
        {
            {
                "ประเภท " + resType.Code + "-" + resType.Name, Url.Action("Index", "MasResourceType")
            }, 
            {
                "หมวด " + resGrp.Code + "-" + resGrp.Name, Url.Action("Index", "MasResourceGroup", new {resTypeId=resGrp.ResTypeId})
            }
        };
    ViewBag.Title = "แสดงกลุ่ม ";// + resGrp.Code + "-" + resGrp.Name;
    ViewBag.Descr = "กลุ่มภายใต้หมวด " + resGrp.Code + "-" + resGrp.Name;
}

@using (Html.BeginForm("Delete", "MasResourceSubGroup", FormMethod.Post, new {@id="form", ViewBag.ResGrpId}))
{
    @Html.AntiForgeryToken()
    <div class="row-fluid">
        
        <div class="block-button-menu-above">
            @Html.ButtonAdd(new{ViewBag.ResGrpId})
            @Html.ButtonDeleteAll()
            <a class="btn button-standard-width btn-small btn-info" data-rel="tooltip" href="@Url.Action("Index", "MasResourceGroup", new {ResTypeId=resGrp.ResTypeId})" role="button" data-original-title="" title=""><i class="icon-folder-close bigger-120"></i> กลับไปที่หมวดพัสดุ</a>
        </div>

        @Html.ValidationSummary(true, "", new
            {
                @class = "alert alert-error"
            })
        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    จำแนกกลุ่มพัสดุภายใต้หมวด [<a href="@Url.Action("Index", "MasResourceGroup", new {ResTypeId=resGrp.ResTypeId})"><span style="color:yellow; font-weight: bolder">  @resGrp.Name </span></a> ]
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width: 50px;">
                        <!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th class="center">
                        @Html.DisplayNameFor(model => model.Code)
                    </th>
                    <th class="center">
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th class="center">
                        @Html.DisplayNameFor(model => model.Descr)
                    </th>
                    <th class="center" style="width:120px">
                        จำนวนรายการ
                    </th>
                    <th class="center" style="width: 80px;">
                        <!-- Empty head column for export buttons -->&nbsp;
                    </th>
                </tr>
            </thead>

            <tbody>
                @foreach (var m in Model)
                {
                    <tr>
                        <td class="center">
                            <!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.ResSubGrpId)" />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td class="center">
                            @Html.ActionLink(@m.Code + " ", "Details", new{Id = m.ResSubGrpId, ResGrpId = ViewBag.ResGrpId})
                        </td>
                        <td>
                            @Html.ActionLink(@m.Name + " ", "Details", new{Id = m.ResSubGrpId, ResGrpId = ViewBag.ResGrpId})
                        </td>
                        <td>
                            @((m.Descr??"").LimitText(80)) 
                        </td>
                        <td class="center">
                            @m.AmountOfItem
                        </td>
                        <td class="td-actions">
                            <div class="hidden-phone visible-desktop btn-group">
                                <a class="btn btn-mini btn-info" data-rel="tooltip" href="@Url.Action("Index", "MasResourceItem", new {@m.ResSubGrpId})" role="button" data-original-title="" title=""><i class="icon-folder-open bigger-120"></i> รายการพัสดุ</a>
                                <a class="btn btn-mini btn-grey" data-rel="tooltip" href="@Url.Action("Details", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})" role="button" data-original-title="" title=""><i class="icon-search bigger-120"></i></a>
                                <a class="btn btn-mini btn-success" data-rel="tooltip" href="@Url.Action("Edit", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})" role="button" data-original-title="" title=""><i class="icon-edit bigger-120"></i></a>
                                <a class="btn btn-mini btn-danger" data-rel="tooltip" onclick="bootbox.confirm('ท่านต้องการยืนยันการลบข้อมูลนี้หรือไม่ ?', function(result) { if (result) window.location.href='@Url.Action("Delete", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})'; });" role="button" data-original-title="" title=""><i class="icon-trash bigger-120"></i></a>
                            </div>
                            <div class="hidden-desktop visible-phone">
	                            <!-- Buttons/Tooltip On Tablet (or small monitor) /-->
	                            <div class="inline position-relative">
		                            <button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown">
			                            <i class="icon-caret-down icon-only bigger-120"></i>
		                            </button>
		                            <ul class="dropdown-menu dropdown-icon-only dropdown-yellow pull-right dropdown-caret dropdown-close">
                                        <li>
                                            <a class="btn btn-mini btn-info" data-rel="tooltip" href="@Url.Action("Index", "MasResourceItem", new {@m.ResSubGrpId})" role="button" data-original-title="" title=""><i class="icon-folder-open bigger-120"></i> รายการพัสดุ</a>
                                        </li>
                                        <li>
                                            <a class="btn btn-mini btn-grey" data-rel="tooltip" href="@Url.Action("Details", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})" role="button" data-original-title="" title=""><i class="icon-search bigger-120"></i></a>
                                        </li>
			                            <li>
				                            <a class="btn btn-mini btn-success" data-rel="tooltip" href="@Url.Action("Edit", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})" role="button" data-original-title="" title=""><i class="icon-edit bigger-120"></i></a>
                                        </li>
			                            <li>
				                            <a class="btn btn-mini btn-danger" data-rel="tooltip" onclick="bootbox.confirm('ท่านต้องการยืนยันการลบข้อมูลนี้หรือไม่ ?', function(result) { if (result) window.location.href='@Url.Action("Delete", new {id=@m.ResSubGrpId,ViewBag.ResGrpId})'; });" role="button" data-original-title="" title=""><i class="icon-trash bigger-120"></i></a>
                                        </li>
		                            </ul>
	                            </div>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@section scripts {
    <script type="text/javascript">
        $('#table').dataTable({
            "aoColumns": [
                { "bSortable": false },
                null,
                null,
                null,
                null,
                { "bSortable": false }
            ]
        });
        $(function() {


            $('table th input:checkbox').on('click', function() {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                    .each(function() {
                        this.checked = that.checked;
                        $(this).closest('tr').toggleClass('selected');
                    });

            });

            $('[data-rel=tooltip]').tooltip();

            $("#form").submit(function(e) {
                e.preventDefault();
                var currentForm = this;
                bootbox.confirm("คุณแน่ใจหรือไม่", function(c) {
                    if (c) {
                        $.ajax({
                            url: "@Url.Action("DeleteAll")",
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            context: document.body,
                            success: function(s) {
                                if (s.success) {
                                    location.reload();
                                }
                            },
                            error: function(xhr, status, error) {
                                alert("Status: " + status + ", Error: " + error + ", XHR: " + xhr.readyState);
                            },
                            statusCode: {
                                404: function() {
                                    alert("Page not found");
                                }
                            }
                        });
                    }
                });
                return false;
            });
        });
    </script>
}